//
//  AIGirlChatMsgTableViewCell.swift
//  AIGirl
//
//  Created by Rae on 2024/1/16.
//

import UIKit

class AIGirlChatReceiveTextCell: UITableViewCell {
    
    private lazy var backCoverView: UIImageView = {
       let view = UIImageView()
        view.image = UIImage(named: "AIGirl_Message_vipBorder")
        view.contentMode = .scaleToFill
        return view
    }()
    
    private lazy var messageLabel   = InsetsLabel().fj
        .font(16.font_medium).color(.white).line(0)
        .end()
    private lazy var lockView = c_make_ex(UIImageView.self)
        .mode(.scaleAspectFill).image("AIGirl_Message_lock").end()
    
    var msgModel: AIGirlMessageItemModel?{
        didSet{
            guard let md = msgModel else { return }
            lockView.isHidden = !md.look
            if md.look {
                backCoverView.image = UIImage(named: "AIGirl_Message_vipBorder")
//                backView.backgroundColor = "#FECF60".toColor.alpha(0.2)
//                backView.layer.borderColor = "#FFF2AB".toColor.cgColor
//                backView.layer.borderWidth = 1
                messageLabel.text = md.msg.maskedString()
            }else{
                backCoverView.image = UIImage(named: "AIGirl_Message_border")
//                backView.backgroundColor = .black.alpha(0.2)
//                backView.layer.borderColor = UIColor.clear.cgColor
//                backView.layer.borderWidth = 1
                messageLabel.text = md.msg
            }
        }
    }
    
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        selectionStyle = .none
        backgroundColor = .clear
        contentView.backgroundColor = .clear
        
        contentView.subviews{
            backCoverView
            messageLabel
            lockView
        }
        
//        backCoverView.backgroundColor = .black.alpha(0.2)
        backCoverView.leading(15).width(<=(CGFloat.screenWidth - 80.0)).top(15.0).bottom(0)
        messageLabel.followEdges(backCoverView)
        messageLabel.contentEdgeInsets = UIEdgeInsets(top: 10.0, left: 20.0, bottom: 10.0, right: 20.0)
        lockView.size(21).centerVertically()
        lockView.Leading == backCoverView.Trailing + 9
        messageLabel.text = "**************"
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
    }

    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

        // Configure the view for the selected state
    }

}

class AIGirlChatReceiveImgCell: UITableViewCell {
    
    private lazy var backView = AIGirlRoundedCornersView(topLeftRa: 30, topRightRa: 15, bottomLeftRa: 5, bottomRightRa: 15)
    private lazy var contentImg   = c_make_ex(UIImageView.self)
        .mode(.scaleAspectFill).end()
    private lazy var VIPImg   = c_make_ex(UIImageView.self)
        .mode(.scaleAspectFill).image("AIGirl_Message_VIPbordor").end()
    private lazy var lockView = c_make_ex(UIImageView.self)
        .mode(.scaleAspectFill).image("AIGirl_Message_lock").end()
    private lazy var lockContent = UIIconButton(style: .leading(itemSpace: 0.5))
    
    var msgModel: AIGirlMessageItemModel?{
        didSet{
            guard let md = msgModel else { return }
            let orUrl = md.msg
            let realUrl = String(orUrl.filter { $0 != "\t" })
            contentImg.loadImage(url: realUrl)
            VIPImg.isHidden = true
            if md.look {
                applyBlurEffect(to: contentImg)
                if md.vip {
                    VIPImg.isHidden = false
                    lockContent.setTitle("", for: .normal)
                    lockContent.setImage("AIGirl_Message_vip".toImage, for: .normal)
                }else{
                    if md.coin > 0 {
                        lockContent.setTitle("\(md.coin)", for: .normal)
                    }else{
                        lockContent.setTitle("0", for: .normal)
                    }
                    lockContent.setImage("AIGirl_Discover_Detail_diamond".toImage, for: .normal)
                }
            }
            lockView.isHidden = !md.look
        }
    }
    
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        selectionStyle = .none
        backgroundColor = .clear
        contentView.backgroundColor = .clear
        
        contentView.subviews{
            backView.subviews{
                contentImg
                VIPImg
                lockView
                lockContent
            }
        }
        
        backView.leading(15).width(135).height(180).top(15.0).bottom(0)
        contentImg.fillContainer()
        VIPImg.fillContainer()
        lockView.size(21).centerHorizontally().centerVertically()
        lockContent.centerHorizontally()
        lockContent.Top == lockView.Bottom + 5
        backView.backgroundColor = .black.alpha(0.2)
        lockContent.style { v in
            v.titleLabel?.font = 9.5.font_semiBold
            v.setTitleColor(.white, for: .normal)
            v.iconSize = CGSize(width: 15, height: 15)
            v.isUserInteractionEnabled = false
        }
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
    }

    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

        // Configure the view for the selected state
    }

    private func applyBlurEffect(to imageView: UIImageView) {
        let blurEffect = UIBlurEffect(style: .regular)
        let blurEffectView = UIVisualEffectView(effect: blurEffect)
        blurEffectView.frame = imageView.bounds
        blurEffectView.autoresizingMask = [.flexibleWidth, .flexibleHeight]

        imageView.addSubview(blurEffectView)
    }
}

class AIGirlChatReceiveLoadingCell: UITableViewCell, AIGirlLottieAnimationProtocol {
    
    private lazy var backView = AIGirlRoundedCornersView(topLeftRa: 30, topRightRa: 15, bottomLeftRa: 5, bottomRightRa: 15)
    private lazy var animateView: AIGirlLottieAnimationView = {
        lottieAnimation.make(.msg_loading)
    }()
    
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        selectionStyle = .none
        backgroundColor = .clear
        contentView.backgroundColor = .clear
        
        contentView.subviews{
            backView.subviews{
                animateView
            }
        }
        
        backView.leading(15).width(60).height(36).top(15.0).bottom(0)
        animateView.leading(10).trailing(10).height(25).centerVertically()
        backView.backgroundColor = .black.alpha(0.2)
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
    }

    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

        // Configure the view for the selected state
    }
}

class AIGirlChatSentTextCell: UITableViewCell {

    private lazy var backView = AIGirlRoundedCornersView(topLeftRa: 15, topRightRa: 30, bottomLeftRa: 15, bottomRightRa: 5)
    private lazy var messageLabel   = InsetsLabel().fj
        .font(16.font_medium).color(.white).line(0)
//        .bgColor(.black.alpha(0.2))
//        .corner(15.0, corners: [.layerMaxXMinYCorner, .layerMinXMaxYCorner, .layerMaxXMaxYCorner])
        .end()
    var msgModel: AIGirlMessageItemModel?{
        didSet{
            guard let md = msgModel else { return }
            messageLabel.text = md.msg
        }
    }
    
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        selectionStyle = .none
        backgroundColor = .clear
        contentView.backgroundColor = .clear
        
        contentView.subviews{
            backView.subviews{
                messageLabel
            }
        }
        
        backView.backgroundColor = "#7F58FF".toColor.alpha(0.3)
        backView.width(<=(CGFloat.screenWidth - 70.0)).trailing(15).top(15.0).bottom(0)
        messageLabel.fillContainer()
//        messageLabel.leading(15).trailing(65).top(15.0).bottom(0)
        messageLabel.contentEdgeInsets = UIEdgeInsets(top: 10.0, left: 20.0, bottom: 10.0, right: 20.0)
//        messageLabel.text = "**********************************************************************************************"
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
    }

    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

        // Configure the view for the selected state
    }

}

class AIGirlChatSentGiftCell: UITableViewCell {

    private lazy var backView = c_make_ex(UIView.self)
        .corner(20).end()
    private lazy var contentImg = c_make_ex(UIImageView.self)
        .mode(.scaleAspectFill).end()
    
    var msgModel: AIGirlMessageItemModel?{
        didSet{
            guard let md = msgModel else { return }
            let orUrl = md.msg
            let realUrl = String(orUrl.filter { $0 != "\t" })
            contentImg.loadImage(url: realUrl)
        }
    }
    
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        
        selectionStyle = .none
        backgroundColor = .clear
        contentView.backgroundColor = .clear
        
        contentView.subviews{
            backView.subviews{
                contentImg
            }
        }
        
        backView.size(115).trailing(15).top(15.0).bottom(0)
        contentImg.fillContainer()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
    }

    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

        // Configure the view for the selected state
    }

}

class AIGirlRoundedCornersView: UIView {
    
    // 圆角半径设置
    private var topLeftRadius: CGFloat = 0.0
    private var topRightRadius: CGFloat = 0.0
    private var bottomLeftRadius: CGFloat = 0.0
    private var bottomRightRadius: CGFloat = 0.0
    
    init(topLeftRa: CGFloat, topRightRa: CGFloat, bottomLeftRa: CGFloat, bottomRightRa: CGFloat) {
        super.init(frame: .zero)
        topLeftRadius = topLeftRa
        topRightRadius = topRightRa
        bottomLeftRadius = bottomLeftRa
        bottomRightRadius = bottomRightRa
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func layoutSubviews() {
        super.layoutSubviews()
        
        

        // 创建路径
        let path = UIBezierPath()
        // 移动到左上角
        path.move(to: CGPoint(x: 0, y: topLeftRadius))
        // 左上角
        path.addArc(withCenter: CGPoint(x: topLeftRadius, y: topLeftRadius),
                    radius: topLeftRadius,
                    startAngle: CGFloat.pi,
                    endAngle: -CGFloat.pi / 2.0,
                    clockwise: true)
        // 右上角
        path.addArc(withCenter: CGPoint(x: bounds.width - topRightRadius, y: topRightRadius),
                    radius: topRightRadius,
                    startAngle: -CGFloat.pi / 2.0,
                    endAngle: 0,
                    clockwise: true)
        // 右下角
        path.addArc(withCenter: CGPoint(x: bounds.width - bottomRightRadius, y: bounds.height - bottomRightRadius),
                    radius: bottomRightRadius,
                    startAngle: 0,
                    endAngle: CGFloat.pi / 2.0,
                    clockwise: true)
        // 左下角
        path.addArc(withCenter: CGPoint(x: bottomLeftRadius, y: bounds.height - bottomLeftRadius),
                    radius: bottomLeftRadius,
                    startAngle: CGFloat.pi / 2.0,
                    endAngle: CGFloat.pi,
                    clockwise: true)
        // 封闭路径
        path.close()
        // 创建 CAShapeLayer
        let shapeLayer = CAShapeLayer()
        shapeLayer.path = path.cgPath
        // 应用到视图
        layer.mask = shapeLayer
    }
}

//class SendRoundedCornersView: UIView {
//    
//    override func layoutSubviews() {
//        super.layoutSubviews()
//        
//        // 圆角半径设置
//        let topLeftRadius: CGFloat = 15.0
//        let topRightRadius: CGFloat = 30.0
//        let bottomLeftRadius: CGFloat = 15.0
//        let bottomRightRadius: CGFloat = 5.0
//
//        // 创建路径
//        let path = UIBezierPath()
//        // 移动到左上角
//        path.move(to: CGPoint(x: 0, y: topLeftRadius))
//        // 左上角
//        path.addArc(withCenter: CGPoint(x: topLeftRadius, y: topLeftRadius),
//                    radius: topLeftRadius,
//                    startAngle: CGFloat.pi,
//                    endAngle: -CGFloat.pi / 2.0,
//                    clockwise: true)
//        // 右上角
//        path.addArc(withCenter: CGPoint(x: bounds.width - topRightRadius, y: topRightRadius),
//                    radius: topRightRadius,
//                    startAngle: -CGFloat.pi / 2.0,
//                    endAngle: 0,
//                    clockwise: true)
//        // 右下角
//        path.addArc(withCenter: CGPoint(x: bounds.width - bottomRightRadius, y: bounds.height - bottomRightRadius),
//                    radius: bottomRightRadius,
//                    startAngle: 0,
//                    endAngle: CGFloat.pi / 2.0,
//                    clockwise: true)
//        // 左下角
//        path.addArc(withCenter: CGPoint(x: bottomLeftRadius, y: bounds.height - bottomLeftRadius),
//                    radius: bottomLeftRadius,
//                    startAngle: CGFloat.pi / 2.0,
//                    endAngle: CGFloat.pi,
//                    clockwise: true)
//        // 封闭路径
//        path.close()
//        // 创建 CAShapeLayer
//        let shapeLayer = CAShapeLayer()
//        shapeLayer.path = path.cgPath
//        // 应用到视图
//        layer.mask = shapeLayer
//    }
//}
